<?php 
declare(strict_types=1);

namespace App\Service;

use App\Exception\BusinessException;
use Hyperf\Di\Annotation\Inject;
use App\Constants\ErrorCode;
use App\Utils\ServerUtil;
use App\Dao\AdminDao;
use App\Utils\ApiUtils;
use App\Dao\RoleAdminDao;
use App\Dao\AdminLogDao;
use Hyperf\Utils\Context;

class ManageService
 { 

    /**
     * @Inject 
     * @var AdminDao
     */
    protected $dao;

    /**
     * @Inject 
     * @var AdminLogDao
     */
    protected $logdao;

    /**
     * @Inject 
     * @var RoleAdminDao
     */
    protected $radao;

    public function list($name,$page,$pageSize){
        $where[] = ['is_del','=' ,1];
        //搜索条件
        if($name){
            $where[] = ['name','like' ,'%'.$name.'%'];
        }
        $list = $this->dao->searchByWherePage($pageSize,$where);
        if(!empty($list)){
            foreach($list as $k => $v){
                unset($list[$k]['password']);
                $list[$k]['update_time'] = empty($v['update_time'])?'':\date('Y-m-d H:i',$v['update_time']);
                $list[$k]['last_login_time'] = empty($v['last_login_time'])?'':\date('Y-m-d H:i',$v['last_login_time']);
                if(in_array($v['id'], config('web.super'))){
                    $list[$k]['del_able'] = 2;
                }else{
                    $list[$k]['del_able'] = 1;
                }
                $list[$k]['role_name'] = $this->dao->manageRole($v['id']);
            }
        }
        $count = $this->dao->countByWhere($where);
        return ['data'=>$list,'count'=>$count];
    }

    public function add($data)
    {
        $where[] = ['name','=' ,$data['name']];
        $where[] = ['is_del','=' ,1];
        $info = $this->dao->searchByWhereFirst($where);
        if($info && $info->id){
            throw new BusinessException(ErrorCode::ERR_BUESSUS,'账号已被占用');
        }
        $role_id = $data['role_id'];
        unset($data['role_id']);
        $data['password'] = ApiUtils::encryption($data['password']);
        $data['add_time'] = time();
        $obj = $this->dao->add($data);
        $obj1 = $this->radao->add(['role_id'=>$role_id,'admin_id'=>$obj->id]);
        if ($obj1) {
            //添加管理员操作日记
            $this->logdao->addLog('添加了管理员,信息为'.json_encode($data));
            return $obj->id;
            
        } else {
            throw new BusinessException(ErrorCode::ERR_BUESSUS,'网络繁忙,请重试'); 
             
        }
    }

    public function editGet($id)
    {
        $where[] = ['id','=' ,$id];
        $obj = $this->dao->searchByWhereFirst($where);
        if($obj){
            $info = $obj->toArray();
            unset($info['password']);
            $info['update_time'] = empty($info['update_time'])?'':\date('Y-m-d H:i',$info['update_time']);
            $info['last_login_time'] = empty($info['last_login_time'])?'':\date('Y-m-d H:i',$info['last_login_time']);
            $info['role_id'] = $this->dao->manageRoleid($id);
            return $info;
        }else{
            throw new BusinessException(ErrorCode::ERR_BUESSUS,'用户不存在'); 
        }
        
    }

    public function editPost($data)
    {
        $where1[] = ['id','=' ,$data['id']];
        $obj = $this->dao->searchByWhereFirst($where1);
        $ori = json_encode($obj->toArray());
        $where[] = ['name','=' ,$data['name']];
        $info = $this->dao->searchByWhereFirst($where);
        if($info && $info->id != $data['id']){
            throw new BusinessException(ErrorCode::ERR_BUESSUS,'账号已被占用'); 
        }
        $role_id = $data['role_id'];
        unset($data['role_id']);
        if(!empty($data['password'])){
            $data['password'] = ApiUtils::encryption($data['password']);
        }else{
            unset($data['password']);
        }
        $data['update_time'] = time();
        $this->dao->update(['id'=>$data['id']],$data);
        $this->radao->delete(['admin_id'=>$data['id']]);
        $obj1 = $this->radao->add(['role_id'=>$role_id,'admin_id'=>$data['id']]);
        //添加管理员操作日记
        $this->logdao->addLog('编辑了管理员,信息由原值'.$ori.'修改成'.json_encode($data));
        return;
  
    }

    //删除管理员
    public function del($id)
    {
        $where[] = ['id','=' ,$id];
        $obj = $this->dao->searchByWhereFirst($where);
        if($obj){
            //日志用的原始值
            $ori = json_encode($obj->toArray());
            $data['is_del'] = 2;
            $data['update_time'] = time();
            $this->dao->update($where,$data);
            $this->radao->delete([$id]);
            //添加管理员操作日记
            $this->logdao->addLog('删除了管理员,信息为'.$ori);
            return;
        }else{
            throw new BusinessException(ErrorCode::ERR_BUESSUS,'用户不存在'); 
        }
        
       
    }

    //重置密码
    public function password($data){
        $user = Context::get('jwt_token');
        $id=$user['uid'];
        $where[] = ['id','=' ,$id];
        $obj = $this->dao->searchByWhereFirst($where);
        $data['password'] = ApiUtils::encryption($data['password']);
        if($data['password']!=$obj['password']){
            throw new BusinessException(ErrorCode::ERR_BUESSUS,'用户旧密码错误'); 
        }
        $this->dao->update(['id'=> $id],['password'=>ApiUtils::encryption($data['password_confirmation'])]);
        //添加管理员操作日记
        $this->logdao->addLog('修改了用户密码');
        return true;
    }

    

 }